﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VirtualTournaments.Frontend.MenuControls.TournamentManagerMenu.InnerControls
{
    using TournamentsService = Services.Tournaments.TournamentsService;
    using Tournament = Services.Tournaments.Tournament;
    using Journey = Services.Tournaments.Journey;
    using Match = Services.Tournaments.Match;

    public partial class PlayMenu : System.Web.UI.UserControl
    {
        private const short TournamentSelectionViewIndex = 0;
        private const short LeagueTournamentPlayInfoViewIndex = 1;
        private const short RoundTournamentPlayInfoViewIndex = 2;
        
        public EventHandler GoBackButtonClicked;


        protected void Page_Load(object sender, EventArgs e)
        {
            LeagueTournamentPlayControl.GoBackButtonClicked += new EventHandler(PlayMenu_GoToMainMenu);
            RoundTournamentPlayControl.GoBackButtonClicked += new EventHandler(PlayMenu_GoToMainMenu);

            //Services.Users.User user = Session["CurrentUser"] as Services.Users.User;
            
            //TODO: what if null?
            //if (user != null)
            //{
                //TODO: should get active tournaments based on logged user!!!
                Tournament[] activeTournaments = TournamentsService.GetActiveTournaments();

                if (activeTournaments.Length > 0)
                {
                    TableRow newRow;
                    TableCell newCell1, newCell2, newCell3;
                    int i = 0;
                    foreach (Tournament tournament in activeTournaments)
                    {
                        newRow = new TableRow();
                        newCell1 = new TableCell();
                        newCell2 = new TableCell();
                        newCell3 = new TableCell();

                        newCell1.Controls.Add(new Label { ID = "Tournament " + i, Text = tournament.Name });
                        newCell1.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                        newCell2.Controls.Add(new Label { Text = tournament.Type });
                        newCell2.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                        Button playButton = new Button { ID = "PlayButton" + i, Text = "Play Matches", BackColor = System.Drawing.Color.Transparent, BorderStyle = System.Web.UI.WebControls.BorderStyle.None };
                        playButton.Click += new EventHandler(PlayButton_OnClick);
                        playButton.Attributes.Add("onmouseover", "this.style.cursor='hand'");
                        newCell3.Controls.Add(playButton);
                        newCell3.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

                        newRow.Cells.Add(newCell1);
                        newRow.Cells.Add(newCell2);
                        newRow.Cells.Add(newCell3);

                        TournamentsTable.Rows.Add(newRow);
                        i++;
                    }

                    ClickToPlayLabel.Text = "Click \"Play Matches\" to play the selected tournament";
                }
                else
                    ClickToPlayLabel.Text = "There are no active tournaments";

            //}
        }

        protected void GoBackButton_OnClick(object sender, EventArgs e)
        {
            //TODO: find a better way to do this
            GoBackButtonClicked(this, new VirtualTournamentsFrontendEvent { SenderMenu = "PlayMenu" });
        }
        
        protected void PlayButton_OnClick(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
            //TODO: what if null?
            if (clickedButton != null)
            {
                int rowIndex = Int32.Parse(clickedButton.ID.Replace("PlayButton", ""));
                rowIndex++;
                string tournamentName = (TournamentsTable.Rows[rowIndex].Cells[0].Controls[0] as Label).Text;

                if (Session["HardcodedJourneys"] == null)
                    Session.Add("HardcodedJourneys", SetJourneys());

                if ((TournamentsTable.Rows[rowIndex].Cells[1].Controls[0] as Label).Text.Equals(Tournament.ALLAGAINSTALL_TYPE))
                {
                    this.LeagueTournamentPlayControl.TournamentName = tournamentName;
                    this.PlayMenuViewControler.ActiveViewIndex = LeagueTournamentPlayInfoViewIndex;
                }
                else 
                {
                    this.RoundTournamentPlayControl.TournamentName = tournamentName;
                    this.PlayMenuViewControler.ActiveViewIndex = RoundTournamentPlayInfoViewIndex;
                }
            }

        }

        protected void PlayMenu_GoToMainMenu(object sender, EventArgs e) 
        {
            this.PlayMenuViewControler.ActiveViewIndex = TournamentSelectionViewIndex;
        }

        private List<Journey> SetJourneys()
        {
            List<Journey> journeys = new List<Journey>();

            /* Journey 1 */
            Match journey1Match1 = new Match("Arsenal", "Argentinos");
            Match journey1Match2 = new Match("Atlético Tucumán", "Banfield");

            Journey journey1 = new Journey();
            journey1.addMatch(journey1Match1);
            journey1.addMatch(journey1Match2);

            /* Journey 2 */
            Match journey2Match1 = new Match("Arsenal", "Atlético Tucumán");
            Match journey2Match2 = new Match("Argentinos", "Banfield");

            Journey journey2 = new Journey();
            journey2.addMatch(journey2Match1);
            journey2.addMatch(journey2Match2);

            /* Journey 3 */
            Match journey3Match1 = new Match("Banfield", "Arsenal");
            Match journey3Match2 = new Match("Atlético Tucumán", "Argentinos");

            Journey journey3 = new Journey();
            journey3.addMatch(journey3Match1);
            journey3.addMatch(journey3Match2);

            journeys.Add(journey1);
            journeys.Add(journey2);
            journeys.Add(journey3);

            return journeys;
        }
    }
}